<?php

namespace App\Service;

use App\Exceptions\OrException;
use App\Models\GuildUser;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;

class GuildUserService
{
    protected $pwdCost = 12;

	
    public function updatePwd(string $password, User $user) : bool
    {
        $password = password_hash($password, PASSWORD_DEFAULT, ['cost' => $this->pwdCost]);
        if ($password === $user->password) return true;
        $user->password = $password;
        return $user->save();
    }
	/**
	 * 添加代理
	 * @param array $data
	 * @return bool
	 * @throws OrException
	 */
    public function createGuild(array $data) : array
    {
		$validator = Validator::make($data, [		
			'gid' => 'required|unique:guild_user,gid',
            'username' => 'required|unique:guild_user,username|between:6,15',
            'password' => 'required|alpha_dash|between:6,20',
			'boss_name' => ['unique:guild_user,boss_name', 'regex:/^([\xe4-\xe9][\x80-\xbf]{2}){2,4}$/'],
            'mobile' => 'regex:/^1[3-9]\d{9}$/|unique:guild_user,mobile',
			'tel' => 'nullable|regex:/^([0-9]{3,4}-)?[0-9]{7,8}$/|unique:guild_user,tel',	
			'wechat' => 'regex:/^[a-zA-Z0-9_]+$/|unique:guild_user,wechat',
			'qq' => 'regex:/^[1-9]\d{4,10}$/|unique:guild_user,qq',	
			'email' => 'regex:/^[a-z0-9]+([._-][a-z0-9]+)*@([0-9a-z]+\.[a-z]{2,14}(\.[a-z]{2})?)$/i|unique:guild_user,email',
			'kefu' => 'required',
			'address' => 'required',				
			//'alipay' => ['unique:guild_user,alipay', 'regex:/^[1-9]\d{4,10}$|(\w[-\w.+]*@(qq|gmail|163|126|139|sohu|aliyun|189)\.(com|cn)(\r\n|\r|\n)?$)/']
        ],[
			'gid.required' => '请输入所属公会',
			'gid.unique' => '所属公会已存在',
            'username.required' => '请输入用户名',
			'username.between' => '用户名长度在6-15之间',
			'username.unique' => '用户名已存在',
			'password.between' => '密码在长度在6-20之间',
			'boss_name.unique' => '公会老板已存在',
			'boss_name.regex' => '公会老板必须是中文'	,
			'mobile.regex' => '手机号不正确',
			'mobile.unique' => '手机号已经存在',
			'tel.regex' => '座机号不正确',
			'tel.unique' => '座机号已经存在',	
			'wechat.regex' => '微信号不正确',
			'wechat.unique' => '微信号已经存在',
			'qq.regex' => 'QQ号不正确',
			'qq.unique' => 'QQ号已经存在',		
			'email.regex' => '邮箱不合法',
			'email.unique' => 'Email号已经存在',
			'kefu.required' =>'请输入指定客服',
			'address.required' =>'请输入地址',	
			//'alipay.unique'	=> '支付宝账号已经存在',
			//'alipay.regex'	=> '支付宝账号输入错误',	
        ]);
        $error = $validator->errors()->first();
		if ($error) {
			return ['status' => -1, 'error' => $error];
		}
		if(empty($data['gid'])) {
			return ['status' => -1, 'error' => '请输入所属公会'];
		}
        $data['status'] = 1;
        $data['password'] = password_hash($data['password'], PASSWORD_DEFAULT, ['cost' => $this->pwdCost]);
		$data['created_at'] = date('Y-m-d H:i:s');
        $guildUser = GuildUser::create($data);
        if ($guildUser) {
			$models = new \App\Models\GuildUserComm();
			$models->gid = $data['gid'];
			$models->guser_id = $guildUser->id;
			$models->save();
            return ['status' => 1, 'error' =>'成功'];
        }
        return ['status' => 0, 'error' =>'失败'];
    }

	/**
	 * 修改代理信息
	 * @param array $data
	 * @param 
	 * @return bool
	 * @throws OrException
	 */
    public function updateGuild(array $data, $guildUser, $id) : array
    {
		$validator = Validator::make($data, [
			'gid' => [Rule::unique('guild_user', 'gid')->ignore($id)],	
            'username' => ['between:6,15', Rule::unique('guild_user', 'username')->ignore($id)],
			'boss_name' => ['regex:/^([\xe4-\xe9][\x80-\xbf]{2}){2,4}$/', Rule::unique('guild_user', 'boss_name')->ignore($id)],
            'mobile' => ['regex:/^1[3-9]\d{9}$/', Rule::unique('guild_user', 'mobile')->ignore($id)],		
			'tel' => ['regex:/^([0-9]{3,4}-)?[0-9]{7,8}$/', Rule::unique('guild_user', 'tel')->ignore($id)],	
			'wechat' => ['regex:/^[a-zA-Z0-9_]+$/', Rule::unique('guild_user', 'wechat')->ignore($id)],
			'qq' => ['regex:/^[1-9]\d{4,10}$/', Rule::unique('guild_user', 'qq')->ignore($id)],	
			'email' => ['regex:/^[a-z0-9]+([._-][a-z0-9]+)*@([0-9a-z]+\.[a-z]{2,14}(\.[a-z]{2})?)$/i', Rule::unique('guild_user', 'email')->ignore($id)],
			'kefu' => 'required',
			'address' => 'required',	
			//'alipay' => ['regex:/^[1-9]\d{4,10}$|(\w[-\w.+]*@(qq|gmail|163|126|139|sohu|aliyun|189)\.(com|cn)(\r\n|\r|\n)?$)/', Rule::unique('agents', 'alipay')->ignore($id)]
        ],[
			'gid.required' => '请输入用户名',
			'gid.unique' => '所属公会已存在',
			'username.between' => '用户名长度在6-15之间',
			'username.unique' => '用户名已使用',	
			'boss_name.regex' => '公会老板输入必须是中文',	
			'boss_name.unique' => '公会老板已使用',
			'mobile.regex' => '手机号不正确',
			'mobile.unique' => '手机号已使用',
			'tel.regex' => '座机号不正确',
			'tel.unique' => '座机号已经存在',	
			'wechat.regex' => '微信号不正确',
			'wechat.unique' => '微信号已使用',
			'qq.regex' => 'QQ号不正确',
			'qq.unique' => 'QQ号已使用',		
			'email.regex' => '邮箱不合法',
			'email.unique' => 'Email号已使用',
			'kefu.required' =>'请输入指定客服',
			'address.required' =>'请输入地址',
        ]);
        $error = $validator->errors()->first();
		if ($error) {
			return ['status' => -1, 'error' => $error];
		}
		if(!empty($data['password'])) {
			if(strlen($data['password']) < 6 || strlen($data['password']) > 20) {
				return ['status' => -1, 'error' => '密码在长度在6-20之间'];
			}
			$data['password'] = password_hash($data['password'], PASSWORD_DEFAULT, ['cost' => $this->pwdCost]);
		} else {
			unset($data['password']);
		}
		$data['updated_at'] = date('Y-m-d H:i:s');
		$res = $guildUser->where('id', $id)->update($data);
        if ($res) {
            return ['status' => 1, 'error' =>'成功'];
        }
        return ['status' => 0, 'error' =>'失败'];
    }

}